import time

from selenium.webdriver.common.by import By
from DBUtils import DBUtils


class SignOperation:
    def sign(self, driver, username, password, repwd):
        # 点击注册链接
        driver.find_element(By.XPATH, '/html/body/div/div/div/div[2]/a').click()
        time.sleep(1)
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入登陆密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # 输入确认密码
        driver.find_element(By.XPATH, "//input[@id='repassword']").send_keys(repwd)
        # 点击注册按钮
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

    # 获取注册成功的实际结果
    def get_success_result(self, driver, username, number):
        time.sleep(1)
        dbutils = DBUtils()
        dbresult = dbutils.select('select username from user')
        print(dbresult)
        title = driver.title
        print(title)
        print((username,))
        length = len(dbresult)
        print(number)
        print(length)
        if length != number:
            if (username,) in dbresult and title == '度小满理财-登录[内测版]':
                return 1
        return 0

    # 获取重复注册的实际结果
    def get_repeat_result(self, username, number):
        time.sleep(1)
        dbutils = DBUtils()
        dbresult = dbutils.select('select username from user')
        length = len(dbresult)
        if length != number:
            if (username,) in dbresult:
                return 1
        return 0

    # 获取两次密码不同的实际结果
    def get_dipa_result(self, username, number):
        time.sleep(1)
        dbutils = DBUtils()
        dbresult = dbutils.select('select username from user')
        length = len(dbresult)
        if length != number:
            if (username,) in dbresult:
                return 1
        return 0

    # 获取缺少确认密码的实际结果
    def get_miss_result(self, driver, username, number):
        time.sleep(1)
        dbutils = DBUtils()
        dbresult = dbutils.select('select username from user')
        tip = driver.find_element(By.XPATH, '/html/body/div/div/div/form/div[3]/span[1]').text
        length = len(dbresult)
        if tip == '重复密码为空' and length != number:
            if (username,) in dbresult:
                return 1
        return 0
